/************************************************************************
 * File: gameloader.cpp                                                 *
 *                                                                      *
 * Copyright 2006, 2008 Colin Kern (colin.kern@gmail.com)               *
 *                                                                      *
 * This file is part of Profit.  Some previous versions were released   *
 * under the names Megacorp Mania and Acquire.                          *
 *                                                                      *
 * Profit is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU General Public License as published by *
 * the Free Software Foundation, either version 3 of the License, or    *
 * any later version.                                                   *
 *                                                                      *
 * Profit is distributed in the hope that it will be useful,            *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with Profit.  If not, see <http://www.gnu.org/licenses/>.      *
 ************************************************************************/
#include "gameloader.h"
#include "game.h"
#include "company.h"
#include <QColor>
#include <QString>

GameLoader::GameLoader(Game *game)
{
	m_game = game;
}
	
bool GameLoader::read(QIODevice *device)
{
	setDevice(device);
	
	while (!atEnd()) {
		readNext();
		
		if (isStartElement()) {
			if (name() == "game") {
				readGame();
			} else {
				raiseError(QObject::tr("This file is not a Profit game configuration"));
			}
		}
	}
	
	return !error();
}

void GameLoader::readGame()
{
	Q_ASSERT(isStartElement() && name() == "game");
	
	while (!atEnd()) {
		readNext();
		
		if (isEndElement()) {
			break;
		}
		
		if (isStartElement()) {
			if (name() == "board") {
				readBoard();
			} else if (name() == "company") {
				readCompany();
			} else if (name() == "settings") {
				readSettings();
			} else {
				readUnknown();
			}
		}
	}
}

void GameLoader::readBoard()
{
	Q_ASSERT(isStartElement() && name() == "board");
	
	int rows = 0;
	int columns = 0;
	
	while (!atEnd()) {
		readNext();
		
		if (isEndElement()) {
			break;
		}
		
		if (isStartElement()) {
			if (name() == "rows") {
				rows = readElementText().toInt();
			} else if (name() == "columns") {
				columns = readElementText().toInt();
			}
		}
	}
	
	m_game->setBoardSize(rows, columns);
}

void GameLoader::readCompany()
{
	Q_ASSERT(isStartElement() && name() == "company");
	
	QString cname;
	Profit::CostGroup group;
	QColor color;
	
	while (!atEnd()) {
		readNext();
		
		if (isEndElement()) {
			break;
		}
		
		if (isStartElement()) {
			if (name() == "name") {
				cname = readElementText();
			} else if (name() == "group") {
				QString text = readElementText().toLower();
				if (text == "low") {
					group = Profit::low;
				} else if (text == "medium") {
					group = Profit::medium;
				} else if (text == "high") {
					group = Profit::high;
				}
			} else if (name() == "color") {
				color = readColor();
			}
		}
	}
	
	m_game->addCompany(new Company(cname, color, group));
}

QColor GameLoader::readColor()
{
	Q_ASSERT(isStartElement() && name() == "color");
	
	int red;
	int green;
	int blue;
	
	while (!atEnd()) {
		readNext();
		
		if (isEndElement()) {
			break;
		}
		
		if (isStartElement()) {
			if (name() == "red") {
				red = readElementText().toInt();
			} else if (name() == "green") {
				green = readElementText().toInt();
			} else if (name() == "blue") {
				blue = readElementText().toInt();
			}
		}
	}
	
	return QColor(red, green, blue);
}

void GameLoader::readSettings()
{
	Q_ASSERT(isStartElement() && name() == "settings");
	
	while (!atEnd()) {
		readNext();
		
		if (isEndElement()) {
			break;
		}
		
		if (isStartElement()) {
			if (name() == "shares") {
				m_game->setInitialShares(readElementText().toInt());
			} else if (name() == "money") {
				m_game->setStartingCash(readElementText().toInt());
			} else if (name() == "tiles") {
				m_game->setTilesInHand(readElementText().toInt());
			} else if (name() == "safesize") {
				m_game->setSafeSize(readElementText().toInt());
			} else if (name() == "buylimit") {
				m_game->setBuyLimit(readElementText().toInt());
			} else if (name() == "monopoly") {
				m_game->setMonopolySize(readElementText().toInt());
			}
		}
	}
}

void GameLoader::readUnknown()
{
	Q_ASSERT(isStartElement());

	while (!atEnd()) {
		readNext();

		if (isEndElement()) {
			break;
		}

		if (isStartElement()) {
			readUnknown();
		}
	}
}